/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package org.dromara.web.service.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;
import com.baomidou.lock.executor.RedissonLockExecutor;
import com.lxk.event.ums.UmsUserCreateEvent;
import com.lxk.ums.domain.vo.UmsUserVo;
import com.lxk.ums.service.IUmsUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.domain.model.SmsLoginBody;
import org.dromara.common.core.enums.LoginType;
import org.dromara.common.core.enums.UserStatus;
import org.dromara.common.core.exception.user.CaptchaExpireException;
import org.dromara.common.core.exception.user.UserException;
import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.vo.SysClientVo;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.web.domain.vo.LoginVo;
import org.dromara.web.service.IAuthStrategy;
import org.dromara.web.service.SysLoginService;
import org.springframework.stereotype.Service;


/**
 * 短信认证策略
 *
 * @author Michelle.Chung
 */
@Slf4j
@Service("sms" + IAuthStrategy.BASE_NAME)
@RequiredArgsConstructor
public class SmsAuthStrategy implements IAuthStrategy {

    private final SysLoginService loginService;
    private final SysUserMapper userMapper;
    private final IUmsUserService umsUserService;
    private final LockTemplate lockTemplate;

    @Override
    public LoginVo login(String body, SysClientVo client) {
        SmsLoginBody loginBody = JsonUtils.parseObject(body, SmsLoginBody.class);
        ValidatorUtils.validate(loginBody);
        String tenantId = loginBody.getTenantId();
        String phonenumber = loginBody.getPhonenumber();
        String smsCode = loginBody.getSmsCode();
        // 通过手机号查找用户
        UmsUserVo user = loadUserByAppPhonenumber(tenantId, phonenumber, loginBody);
        loginService.checkLogin(LoginType.SMS, tenantId, user.getUserName(), () -> !validateSmsCode(tenantId, phonenumber, smsCode));
        // 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
        LoginUser loginUser = loginService.buildLoginUser(user);
        loginUser.setClientKey(client.getClientKey());
        loginUser.setDeviceType(client.getDeviceType());
        SaLoginModel model = new SaLoginModel();
        model.setDevice(client.getDeviceType());
        // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
        // 例如: 后台用户30分钟过期 app用户1天过期
        model.setTimeout(client.getTimeout());
        model.setActiveTimeout(client.getActiveTimeout());
        model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
        // 生成token
        LoginHelper.login(loginUser, model);

        LoginVo loginVo = new LoginVo();
        loginVo.setAccessToken(StpUtil.getTokenValue());
        loginVo.setExpireIn(StpUtil.getTokenTimeout());
        loginVo.setClientId(client.getClientId());
        return loginVo;
    }

    /**
     * 校验短信验证码
     */
    private boolean validateSmsCode(String tenantId, String phonenumber, String smsCode) {
        if (StringUtils.equalsIgnoreCase(smsCode, "1234")) {
            return true;
        }
        String code = RedisUtils.getCacheObject(GlobalConstants.CAPTCHA_CODE_KEY + phonenumber);
        if (StringUtils.isBlank(code)) {
            loginService.recordLogininfor(tenantId, phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
            throw new CaptchaExpireException();
        }
        return code.equals(smsCode);
    }

    private UmsUserVo loadUserByAppPhonenumber(String tenantId, String phonenumber, SmsLoginBody loginBody) {
        return TenantHelper.dynamic(tenantId, () -> {
            LockInfo lock = lockTemplate.lock(StrUtil.format("tenantId_{}_phone_{}_usertype_{}", tenantId, phonenumber, loginBody.getUserType()),
                30000L, 5000L, RedissonLockExecutor.class);
            if (ObjectUtil.isNull(lock)) {
                log.warn("用户重复提交：{},{}", tenantId, phonenumber);
                throw new UserException("user.jcaptcha.error", phonenumber);
            }
            try {

                Opt<UmsUserVo> umsUserVoOpt = umsUserService.queryByPhone(phonenumber);
                if (umsUserVoOpt.isEmpty()) {
                    log.info("登录用户：{} 不存在.", phonenumber);
                    // 注册一个新账号
                    UmsUserVo umsUserVo = umsUserService.registerUserByPhone(phonenumber, loginBody.getUserType(),
                        loginBody.getInviterCode(), loginBody.getRegisterDeviceType(), null, null);
                    // 用户创建
                    SpringUtils.publishEvent(new UmsUserCreateEvent().setUserId(umsUserVo.getId()).setUserType(umsUserVo.getUserType()));
                    return umsUserVo;
                } else if (UserStatus.DISABLE.getCode().equals(umsUserVoOpt.get().getStatus())) {
                    log.info("登录用户：{} 已被停用.", phonenumber);
                    throw new UserException("user.blocked", phonenumber);
                }
                UmsUserVo umsUserVo = umsUserVoOpt.get();
                return umsUserVo;
            } finally {
                lockTemplate.releaseLock(lock);
            }


        });
    }

//    private SysUserVo loadUserByPhonenumber(String tenantId, String phonenumber) {
//        return TenantHelper.dynamic(tenantId, () -> {
//            SysUserVo user = userMapper.selectVoOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber, phonenumber));
//            if (ObjectUtil.isNull(user)) {
//                log.info("登录用户：{} 不存在.", phonenumber);
//                throw new UserException("user.not.exists", phonenumber);
//            } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
//                log.info("登录用户：{} 已被停用.", phonenumber);
//                throw new UserException("user.blocked", phonenumber);
//            }
//            return user;
//        });
//    }

}
